package com.meritit.config;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.meritit.config.annotation.MyLog;
import com.meritit.config.event.ContentEvent;
import com.meritit.config.factory.AutoConfigFactory;
import com.meritit.config.factory.AutoConfigFactoryImpl;

@SpringBootApplication
@EnableConfigServer
@EnableConfigurationProperties
@RestController
public class CloudConfigJdbcServerApplication {

	@Autowired
	private Environment env;
	
	@Autowired
	private AutoConfigFactory acf;
	
	@Bean
	public AutoConfigFactory autoConfigFactory() {
		return new AutoConfigFactoryImpl();
	}
	
	@MyLog("测试")
	@RequestMapping("/read")
	public Map<String, String> readConfig(){
		
		System.out.println(acf);
		
		Map<String, String> map = new HashMap<String, String>() {
			{
				put("local.url", env.getProperty("local.url"));
				put("local.name", env.getProperty("local.name"));
			}
		};
		return map;
	}
	
	
	public static void main(String[] args) {
		ApplicationContext applicationContext = SpringApplication.run(CloudConfigJdbcServerApplication.class, args);
		applicationContext.publishEvent(new ContentEvent("今年是龙年的博客更新了"));
//		System.out.println(SpringFactoriesLoader.loadFactories(AutoConfigFactory.class, null));
		/*BeanConfig bc = context.getBean(BeanConfig.class);
		System.out.println(bc.getUrl());*/
	}
	
}
